﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OnlineManagement.IDAL;
using OnlineManagement.DBUtility;//Please add references
namespace OnlineManagement.SQLServerDAL.Dal
{
	/// <summary>
	/// 数据访问类:Piglets
	/// </summary>
	public partial class Piglets:IPiglets
	{
        public Piglets()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int PigletID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Piglets");
            strSql.Append(" where PigletID=@PigletID");
            SqlParameter[] parameters = {
					new SqlParameter("@PigletID", SqlDbType.Int,4)
			};
            parameters[0].Value = PigletID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据,只能更新品种和阉割日期
        /// </summary>
        public bool Update(OnlineManagement.Model.Piglets model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Piglets set ");
            //strSql.Append("HoggeryID=@HoggeryID,");
            //strSql.Append("SowID=@SowID,");
            //strSql.Append("PigPenID=@PigPenID,");
            //strSql.Append("PhaseID=@PhaseID,");
            strSql.Append("TypeID=@TypeID,");
            //strSql.Append("Birthday=@Birthday,");
            //strSql.Append("SourceID=@SourceID,");
            //strSql.Append("DateJoinFarm=@DateJoinFarm,");
            //strSql.Append("IsInFarm=@IsInFarm,");
            //strSql.Append("Account=@Account,");
            strSql.Append("IsCastrate=@IsCastrate,");
            strSql.Append("CastrateDate=@CastrateDate,");
            //strSql.Append("IsPiglets=@IsPiglets,");
            strSql.Append("Comment=@Comment");
            strSql.Append(" where PigletID=@PigletID");
            SqlParameter[] parameters = {
					//new SqlParameter("@HoggeryID", SqlDbType.Int,4),
					//new SqlParameter("@SowID", SqlDbType.UniqueIdentifier,16),
					//new SqlParameter("@PigPenID", SqlDbType.UniqueIdentifier,16),
					//new SqlParameter("@PhaseID", SqlDbType.Int,4),
					new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier,16),
					//new SqlParameter("@Birthday", SqlDbType.Date,3),
					//new SqlParameter("@SourceID", SqlDbType.TinyInt,1),
					//new SqlParameter("@DateJoinFarm", SqlDbType.Date,3),
					//new SqlParameter("@IsInFarm", SqlDbType.Bit,1),
					//new SqlParameter("@Account", SqlDbType.TinyInt,1),
					new SqlParameter("@IsCastrate", SqlDbType.Bit,1),
					new SqlParameter("@CastrateDate", SqlDbType.Date,3),
					//new SqlParameter("@IsPiglets", SqlDbType.Bit,1),
					new SqlParameter("@Comment", SqlDbType.NVarChar,500),
					new SqlParameter("@PigletID", SqlDbType.Int,4)};
            parameters[0].Value = model.TypeID;
            parameters[1].Value = model.IsCastrate;
            parameters[2].Value = model.CastrateDate;
            parameters[3].Value = model.Comment;
            parameters[4].Value = model.PigletID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int PigletID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Piglets ");
            strSql.Append(" where PigletID=@PigletID");
            SqlParameter[] parameters = {
					new SqlParameter("@PigletID", SqlDbType.Int,4)
			};
            parameters[0].Value = PigletID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string PigletIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Piglets ");
            strSql.Append(" where PigletID in (" + PigletIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OnlineManagement.Model.Piglets GetModel(int PigletID,Guid sowID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 PigletID,HoggeryID,SowID,PigPenID,PhaseID,TypeID,Birthday,SourceID,DateJoinFarm,IsInFarm,Account,IsCastrate,CastrateDate,IsPiglets,Comment from Piglets ");
            strSql.Append(" where PigletID=@PigletID and SowID=@sowID");
            SqlParameter[] parameters = {
					new SqlParameter("@PigletID", SqlDbType.Int,4),
                    new SqlParameter("@sowID",SqlDbType.UniqueIdentifier,16)
			};
            parameters[0].Value = PigletID;
            parameters[1].Value = sowID;

            OnlineManagement.Model.Piglets model=new Model.Piglets() ;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds != null && ds.Tables != null && ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OnlineManagement.Model.Piglets DataRowToModel(DataRow row)
        {
            OnlineManagement.Model.Piglets model = new OnlineManagement.Model.Piglets();
            if (row != null)
            {
                if (row["PigletID"] != null && row["PigletID"].ToString() != "")
                {
                    model.PigletID = int.Parse(row["PigletID"].ToString());
                }
                if (row["HoggeryID"] != null && row["HoggeryID"].ToString() != "")
                {
                    model.HoggeryID = int.Parse(row["HoggeryID"].ToString());
                }
                if (row["SowID"] != null && row["SowID"].ToString() != "")
                {
                    model.SowID = new Guid(row["SowID"].ToString());
                }
                if (row["PigPenID"] != null && row["PigPenID"].ToString() != "")
                {
                    model.PigPenID = new Guid(row["PigPenID"].ToString());
                }
                if (row["PhaseID"] != null && row["PhaseID"].ToString() != "")
                {
                    model.PhaseID = int.Parse(row["PhaseID"].ToString());
                }
                if (row["TypeID"] != null && row["TypeID"].ToString() != "")
                {
                    model.TypeID = new Guid(row["TypeID"].ToString());
                }
                if (row["Birthday"] != null && row["Birthday"].ToString() != "")
                {
                    model.Birthday = DateTime.Parse(row["Birthday"].ToString());
                }
                if (row["SourceID"] != null && row["SourceID"].ToString() != "")
                {
                    model.SourceID = int.Parse(row["SourceID"].ToString());
                }
                if (row["DateJoinFarm"] != null && row["DateJoinFarm"].ToString() != "")
                {
                    model.DateJoinFarm = DateTime.Parse(row["DateJoinFarm"].ToString());
                }
                if (row["IsInFarm"] != null && row["IsInFarm"].ToString() != "")
                {
                    if ((row["IsInFarm"].ToString() == "1") || (row["IsInFarm"].ToString().ToLower() == "true"))
                    {
                        model.IsInFarm = true;
                    }
                    else
                    {
                        model.IsInFarm = false;
                    }
                }
                if (row["Account"] != null && row["Account"].ToString() != "")
                {
                    model.Account = int.Parse(row["Account"].ToString());
                }
                if (row["IsCastrate"] != null && row["IsCastrate"].ToString() != "")
                {
                    if ((row["IsCastrate"].ToString() == "1") || (row["IsCastrate"].ToString().ToLower() == "true"))
                    {
                        model.IsCastrate = true;
                    }
                    else
                    {
                        model.IsCastrate = false;
                    }
                }
                if (row["CastrateDate"] != null && row["CastrateDate"].ToString() != "")
                {
                    model.CastrateDate = DateTime.Parse(row["CastrateDate"].ToString());
                }
                if (row["IsPiglets"] != null && row["IsPiglets"].ToString() != "")
                {
                    if ((row["IsPiglets"].ToString() == "1") || (row["IsPiglets"].ToString().ToLower() == "true"))
                    {
                        model.IsPiglets = true;
                    }
                    else
                    {
                        model.IsPiglets = false;
                    }
                }
                if (row["Comment"] != null)
                {
                    model.Comment = row["Comment"].ToString();
                }
            }
            return model;
        }
        public DataSet GetList(string strWhere)
        {
            throw new NotImplementedException();
        }

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// run procedure get piglets by hoggeryId
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <returns></returns>
        public DataSet GetPigletsByHoggeryID(int hoggeryId,int pageIndex,int pageSize)
        {
            string proceName = Common.PubProcedures.GetPigletsByHoggeryIDProce;
            SqlParameter[] parameters = { 
                                            new SqlParameter("@hoggeryId",SqlDbType.Int,4),
                                            new SqlParameter("@PageIndex",SqlDbType.Int,4),
                                            new SqlParameter("@PageSize",SqlDbType.Int,5)
                                        };
            parameters[0].Value = hoggeryId;
            parameters[1].Value = pageIndex;
            parameters[2].Value = pageSize;
            DataSet ds = DbHelperSQL.RunProcedure(proceName, parameters, "piglets");
            return ds;
        }
        #endregion  ExtensionMethod


       


       
    }
}

